using System;
using System.Collections.Generic;
using System.Text;

namespace SysTables
{

    /*
     * Zoznam vytvorenych databaz a metody na pracu s nimi.
     */
    public class Sysdatabases
    {

        const string SysdatabasesFile = "Sysdatabases.tbl";

   

        public Sysdatabases(bool CreateNew)
        {


            if (CreateNew)
            {
                mDatabases = new List<Database>();
                mSystables = new Systables(CreateNew);
            }
            else Load();

            
        }



        public Database FindDatabase(String name)
        {
            int index = -1;
            return FindDatabase(name, out index);
        }

        public Database FindDatabase(string name, out int index)
        {
            index = -1;
            for (int i = 0; i < mDatabases.Count; i++)
            {
                if (mDatabases[i].Name == name)
                {
                    index = i;
                    return mDatabases[i];
                }
            }
            return null;
        }


        public bool CreateDB(string name)
        {
            if (name == null) return false;
            if (Common.ContainsEmptyCharacters(name)) return false;
            if (FindDatabase(name) != null) return false;
            Database database = new Database(mDatabases.Count, name,this);
            mDatabases.Add(database);
            Save();
            return true;
        }

        public bool DropDB(string name)
        {
            int index;
            Database database = FindDatabase(name, out index);
            if (database==null) return false;

            foreach (Table table in database.Tables)
                mSystables.DropTab(table.TableName);
            mDatabases.RemoveAt(index);
            return true;
        }

        public bool OpenDB(string name)
        {
            int index;
            Database database = FindDatabase(name, out index);
            if (database == null) return false;
            mSystables.CurrentDatabase = database;
            mOpenedDatabase = database;
            return true;
        }

        public bool CloseDB(string name)
        {
            int index;
            Database database = FindDatabase(name, out index);
            if (database == null) return false;
            mSystables.CurrentDatabase = null;
            mOpenedDatabase = null;
            Save();
            return true;
        }



        public void Save()
        {
            File.Save(SysdatabasesFile, Common.Serialize(mDatabases));
            mSystables.Save();
        }

        public void Load()
        {
            if (System.IO.File.Exists(SysdatabasesFile))
                mDatabases = (List<Database>)Common.Deserialize(File.Read(SysdatabasesFile));
            else mDatabases = new List<Database>();

            mSystables = new Systables(false);
            foreach (Database database in mDatabases)
            {
                database.Parent = this;
                database.Reload(mSystables);
            }
        }

        public Systables Systables
        {
            get { return mSystables; }
        }

        public List<Database> Databases
        {
            get { return mDatabases; }
        }

        private Database mOpenedDatabase;

        public Database OpenedDatabase
        {
            get { return mOpenedDatabase; }
            set { mOpenedDatabase = value; }
        }


        List<Database> mDatabases;

        [NonSerialized]
        Systables mSystables;
    }
}
